package com.sumfun.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.mapper.ArticleCategoryReferencedMapper;
import com.sumfun.blog.model.domain.ArticleCategoryReferenced;
import com.sumfun.blog.model.domain.ArticleTagReferenced;
import com.sumfun.blog.model.request.ArticleUpdateRequest;
import com.sumfun.blog.service.ArticleCategoryReferencedService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 17337
* @description 针对表【article_category_referenced(文章标签 )】的数据库操作Service实现
* @createDate 2025-01-09 15:35:02
*/
@Service
public class ArticleCategoryReferencedServiceImpl extends ServiceImpl<ArticleCategoryReferencedMapper, ArticleCategoryReferenced>
    implements ArticleCategoryReferencedService{

    @Override
    public Map<Long, List<ArticleCategoryReferenced>> getByArticleIds(List<Long> articleIds) {
        return articleIds.stream()
                .collect(Collectors.toMap(
                        articleId -> articleId, // keyMapper，使用文章ID作为键
                        articleId -> { // valueMapper，为每个文章ID查询相关的ArticleCategoryReferenced列表
                            QueryWrapper<ArticleCategoryReferenced> queryWrapper = new QueryWrapper<>();
                            queryWrapper.eq("article_id", articleId);
                            return this.list(queryWrapper);
                        },
                        (existing, replacement) -> existing
                ));
    }

    @Override
    public List<Long> getCategoryIdsByArticleId(Long articleId) {
        QueryWrapper<ArticleCategoryReferenced> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId);
        List<ArticleCategoryReferenced> articleCategoryReferencedList = this.list(queryWrapper);
        return articleCategoryReferencedList.stream().map(articleCategoryReferenced -> {
            return articleCategoryReferenced.getCategoryId();
        }).collect(Collectors.toList());
    }

    public Boolean deleteByArticleId(Long articleId) {
        QueryWrapper<ArticleCategoryReferenced> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId);
        boolean remove = this.remove(queryWrapper);
        return remove;
    }

    @Override
    public void updateArticle(ArticleUpdateRequest articleUpdateRequest) {
        if(articleUpdateRequest == null || articleUpdateRequest.getArticleId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long articleId = articleUpdateRequest.getArticleId();
        this.deleteByArticleId(articleId);
        // 获取新的标签ID列表并处理
        List<Long> newcategoryIdList = articleUpdateRequest.getCategoryIdList();
        if (newcategoryIdList != null && !newcategoryIdList.isEmpty()) {
            // 创建新的文章标签关联实体列表
            List<ArticleCategoryReferenced> newArticlecategoryList = newcategoryIdList.stream()
                    .map(categoryId -> {
                        ArticleCategoryReferenced articlecategoryReferenced = new ArticleCategoryReferenced();
                        articlecategoryReferenced.setArticleId(articleId);
                        articlecategoryReferenced.setCategoryId(categoryId);
                        return articlecategoryReferenced;
                    })
                    .collect(Collectors.toList());
            // 批量保存新的文章标签关联
            this.saveBatch(newArticlecategoryList);
        }
    }
}




